<template>
  <el-dialog
    :title="!subwayForm.subwayId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="subwayForm" :rules="dataRule" ref="subwayForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地铁线 如 1号线" prop="subwayLine">
      <el-input v-model="subwayForm.subwayLine" placeholder="地铁线 如 1号线"></el-input>
    </el-form-item>
    <el-form-item label="地区id(省份id)" prop="shenId">
      <el-input v-model="subwayForm.shenId" placeholder="地区id(省份id)"></el-input>
    </el-form-item>
    <el-form-item label="地区id(市id)" prop="shiId">
      <el-input v-model="subwayForm.shiId" placeholder="地区id(市id)"></el-input>
    </el-form-item>
    <el-form-item label="地址区域  广东省-深圳市" prop="address">
      <el-input v-model="subwayForm.address" placeholder="地址区域  广东省-深圳市"></el-input>
    </el-form-item>
    <el-form-item label="状态  0-正常 1-禁用" prop="status">
      <el-input v-model="subwayForm.status" placeholder="状态  0-正常 1-禁用"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        subwayForm: {
          subwayId: 0,
          subwayLine: '',
          shenId: '',
          shiId: '',
          address: '',
          status: ''
        },
        dataRule: {
          subwayLine: [
            { required: true, message: '地铁线 如 1号线不能为空', trigger: 'blur' }
          ],
          shenId: [
            { required: true, message: '地区id(省份id)不能为空', trigger: 'blur' }
          ],
          shiId: [
            { required: true, message: '地区id(市id)不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址区域  广东省-深圳市不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态  0-正常 1-禁用不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.subwayForm.subwayId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['subwayForm'].resetFields()
          if (this.subwayForm.subwayId) {
            this.$http({
              url: this.$http.adornUrl(`/house/subway/info/${this.subwayForm.subwayId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.subwayForm.subwayLine = data.subway.subwayLine
                this.subwayForm.shenId = data.subway.shenId
                this.subwayForm.shiId = data.subway.shiId
                this.subwayForm.address = data.subway.address
                this.subwayForm.status = data.subway.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['subwayForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/subway/${!this.subwayForm.subwayId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'subwayId': this.subwayForm.subwayId || undefined,
                'subwayLine': this.subwayForm.subwayLine,
                'shenId': this.subwayForm.shenId,
                'shiId': this.subwayForm.shiId,
                'address': this.subwayForm.address,
                'status': this.subwayForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
